<template>
    <div class="login_container">
      <div class="login_box">
        <div class="avatar_box">
          <img src="../assets/logo.png" alt="avator" />
        </div>

        <!-- 登录表单区域 -->
        <el-form ref="loginFormRef" :model="loginForm" :rules="loginFormRules" class="login_form" label-width="0px">
          <!-- 用户名 -->
          <el-form-item prop="username" >
            <el-input  v-model="loginForm.username"  prefix-icon="iconfont icon-zhanghao"></el-input>
          </el-form-item>
          <!-- 密码 -->
          <el-form-item prop="password">
            <el-input type="password" v-model="loginForm.password"  prefix-icon="iconfont icon-mima"></el-input>
          </el-form-item>
          <!-- 按钮 -->
          <el-form-item class="btns">
             <el-button :plain="false" type="primary" @click="login">登录</el-button>
             <el-button type="info" @click="resetLoginForm">重置</el-button>

          </el-form-item>
        </el-form>

      </div>
    </div>
</template>

<script>
export default {
  data () {
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },

      // Form验证规则对象
      loginFormRules: {
        // 用户名验证
        username: [
          {
            required: true,
            message: '请输入用户名',
            triggle: 'blur'
          },
          {
            min: 3,
            max: 10,
            message: '长度在3到10个字符',
            triggle: 'blur'

          }
        ],
        // 密码验证
        password: [
          {
            required: true,
            message: '请输入密码',
            triggle: 'blur'
          },
          {
            min: 6,
            max: 20,
            message: '长度在8到20个字符',
            triggle: 'blur'

          }
        ]
      }
    }
  },
  methods: {
    resetLoginForm () {
      // console.log(this)
      this.$refs.loginFormRef.resetFields()
    },
    login () {
      // 登录前验证
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) {} else {
          // console.log(this)
          const { data: result } = await this.$http.post('login', this.loginForm)

          if (result.meta.status !== 200) {
            console.log(result.meta.msg)
            this.$message.error('登录失败')
          } else {
            var token = result.data.token
            window.sessionStorage.setItem('token', token)
            this.$message.success('登录成功')
            this.$router.push('/home')
          }
        }
      })
    }

  }

}
</script>

<style lang="less" scoped>
  .login_container{
    background-color: #2b4b6b;
    height: 100%;
  }

  .login_box{
    width: 40%;
    height: 50%;
    background-color: #fff;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    .avatar_box{
      height: 200px;
      width: 200px;
      border: 1px solid #eee;
      border-radius: 50%;
      padding: 10px;
      box-shadow: 0 0 10px #ddd;
      background-color: #eee;
      position: absolute;
      left: 50%;
      transform: translate(-50%,-50%);
      img{
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: #eee;
      }
    }
  }
  .login_form{
    width: 80%;
    padding: 0 20px;
    position: absolute;
    // 修改默认表单sizing
    box-sizing: border-box;
    left: 50%;
    transform: translate(-50%);
    bottom: 10%;
  }

  .btns{
    display: flex;
    justify-content: flex-end;
  }

</style>
